/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ServerWindows;

import javax.swing.JOptionPane;

/**
 *
 * @author Mohamad
 */
public class MessageWindow extends javax.swing.JFrame {

    /**
     * Creates new form MessageWindow
     */
    public MessageWindow() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        messageTextAreaJScrollPane = new javax.swing.JScrollPane();
        messageJTextArea = new javax.swing.JTextArea();
        sendJButton = new javax.swing.JButton();
        toJLabel = new javax.swing.JLabel();
        toJComboBox = new javax.swing.JComboBox();
        cancelJButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Message Composer");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                messageWindowClosing(evt);
            }
        });

        messageTextAreaJScrollPane.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        messageJTextArea.setColumns(20);
        messageJTextArea.setRows(5);
        messageJTextArea.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        messageTextAreaJScrollPane.setViewportView(messageJTextArea);

        sendJButton.setText("Send");
        sendJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendJButtonActionPerformed(evt);
            }
        });

        toJLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        toJLabel.setText("To :");

        toJComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All Clients", "Online Clients", "Offline Clients" }));

        cancelJButton.setText("Cancel");
        cancelJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelJButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(messageTextAreaJScrollPane)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(toJLabel)
                        .addGap(29, 29, 29)
                        .addComponent(toJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(201, Short.MAX_VALUE)
                        .addComponent(sendJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 145, Short.MAX_VALUE)
                        .addComponent(cancelJButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(toJLabel)
                    .addComponent(toJComboBox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(messageTextAreaJScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sendJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cancelJButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void confirmationOnExit() {
        if (! messageJTextArea.getText().isEmpty()) {
            if (JOptionPane.showConfirmDialog(this, "Are you sure you want to exit", "Exit without sending", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                this.dispose();
            }
        }
        else {
            this.dispose();
        }
    }
    private void sendJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendJButtonActionPerformed
        // TODO add your handling code here:
       // JOptionPane sendConfirmOptionPane = new JOptionPane("This message will be sent to " + toJComboBox.getSelectedItem().toString(), JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
        final int MSG_MAX_SIZE=500;
        if(messageJTextArea.getText().length()>=MSG_MAX_SIZE) { //maximum length (also set as the same on database).
            JOptionPane.showMessageDialog(this,"The message is "+(messageJTextArea.getText().length()-MSG_MAX_SIZE)+"charcters longer than the maximum ("+MSG_MAX_SIZE+")", "Message Max Size Exceded", JOptionPane.ERROR_MESSAGE);            
        }
        else {
            if (JOptionPane.showConfirmDialog(this, "Are you sure you want to send this message to\" " + toJComboBox.getSelectedItem().toString()+"\"", "Confirmation", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            /*
             * CODE HERE 
             * 1- send the message to all clients
             * 2- on sucess, show message and close window.
             *     on faliure, show message and keep window so he can try again.
             */
            }
        } 
        
    }//GEN-LAST:event_sendJButtonActionPerformed

    private void messageWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_messageWindowClosing
        // TODO add your handling code here:
        confirmationOnExit();            
    }//GEN-LAST:event_messageWindowClosing

    private void cancelJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelJButtonActionPerformed
        // TODO add your handling code here:
        confirmationOnExit();
    }//GEN-LAST:event_cancelJButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MessageWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MessageWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MessageWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MessageWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MessageWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelJButton;
    private javax.swing.JTextArea messageJTextArea;
    private javax.swing.JScrollPane messageTextAreaJScrollPane;
    private javax.swing.JButton sendJButton;
    private javax.swing.JComboBox toJComboBox;
    private javax.swing.JLabel toJLabel;
    // End of variables declaration//GEN-END:variables
}
